using Microsoft.AspNetCore.Mvc;
using ERPBackend.Data;
using ERPBackend.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ERPBackend.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PermissionsController : ControllerBase
    {
        private readonly ApplicationDbContext _context;

        public PermissionsController(ApplicationDbContext context)
        {
            _context = context;
        }

        // GET: api/Permissions
        [HttpGet]
        public async Task<ActionResult<IEnumerable<RolePermission>>> GetPermissions()
        {
            // 获取所有不重复的权限
            var permissions = await _context.RolePermissions
                .Select(rp => new RolePermission
                {
                    Id = rp.Id,
                    PermissionName = rp.PermissionName,
                    Description = rp.Description,
                    Status = rp.Status
                })
                .Distinct()
                .ToListAsync();

            return Ok(permissions);
        }
    }
}